package com.bysj.yqsht.service.impl;

import com.bysj.yqsht.bean.*;
import com.bysj.yqsht.bean.extend.CheckTeacherExtend;
import com.bysj.yqsht.dao.CheckTeacherMapper;
import com.bysj.yqsht.dao.CheckallTeacherMapper;
import com.bysj.yqsht.dao.ClassesMapper;
import com.bysj.yqsht.dao.TeacherMapper;
import com.bysj.yqsht.dao.extend.CheckTeacherExtendMapper;
import com.bysj.yqsht.dao.extend.CheckallTeacherExtendMapper;
import com.bysj.yqsht.dao.extend.StudentExtendMapper;
import com.bysj.yqsht.service.ICheckStudentService;
import com.bysj.yqsht.service.ICheckTeacherService;
import com.bysj.yqsht.service.ICheckallStudentService;
import com.bysj.yqsht.utils.CustomerException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ICheckTeacherServiceImpl implements ICheckTeacherService {
    @Resource
    private CheckTeacherMapper checkTeacherMapper;
    @Resource
    private CheckTeacherExtendMapper checkTeacherExtendMapper;
    @Resource
    private TeacherMapper teacherMapper;
    @Resource
    private ClassesMapper classesMapper;
    @Resource
    private CheckallTeacherExtendMapper checkallTeacherExtendMapper;
    @Resource
    private CheckallTeacherMapper checkallTeacherMapper;
    @Resource
    private StudentExtendMapper studentExtendMapper;

    @Resource
    private ICheckStudentService CheckStudentService;



    @Override
    public List<CheckTeacher> findAll() {
        CheckTeacherExample example =new CheckTeacherExample();
        return checkTeacherMapper.selectByExample(example);
    }

    @Override
    public void saveOrUpdate(CheckTeacher checkTeacher) {
        Classes c1 = classesMapper.selectByPrimaryKey(checkTeacher.getClassId());
        if(c1.getTeacherId().equals(checkTeacher.getTeacherId())){
            CheckallTeacher ct1 = checkallTeacherExtendMapper.findWithAllByTeacher(checkTeacher.getClassId());
//            创建一个学生对象作为查询参数
            Student student = new Student();
            student.setClassId(checkTeacher.getClassId());
//            根据该缺勤老师的ClassId查询出该id下所有的学生信息
            List<Student> students = studentExtendMapper.selectAllStudent(student);
//            拿到该classId下的所有学生后 遍历学生集合
            for (Student stu:
                 students) {
//                创建学生考勤记录对象
                CheckStudent checkallStudent = new CheckStudent();
                checkallStudent.setClassId(stu.getClassId());
                checkallStudent.setStudentId(stu.getId());
//                调用给学生添加考勤记录方法
                CheckStudentService.saveOrUpdate(checkallStudent);
            }
            if(ct1 == null){
                CheckallTeacher checkallTeacher = new CheckallTeacher();
                checkallTeacher.setClassesId(checkTeacher.getClassId());
                checkallTeacher.setTeacherId(checkTeacher.getTeacherId());
                checkallTeacher.setNumber("1");
                checkallTeacherMapper.insert(checkallTeacher);
            }else {
                Integer num = Integer.parseInt(ct1.getNumber());
                num++;
                ct1.setNumber(num.toString());
                checkallTeacherMapper.updateByPrimaryKey(ct1);
            }
        }else {
            throw  new CustomerException(" 选择的班级和其指导教师不一致，请审核！");
        }
       // Teacher t1 = teacherMapper.selectByPrimaryKey(checkTeacher.getTeacherId());



//        if(s1.getId().equals(checkStudent.getClassId())){
//            //   checkStudentMapper.insert(checkStudent);
//            CheckallStudent c1=  checkallStudentExtendMapper.findWithAllByStudentId(checkStudent.getStudentId());
////        判断是否存在该ID的学生信
//            CheckallStudent checkallStudent = new CheckallStudent();
//            checkallStudent.setStudentId(checkStudent.getStudentId());
//            checkallStudent.setClassesId(checkStudent.getClassId());
//            if (c1 != null){
//                checkallStudent.setId(c1.getId());
//            }
//            if(c1 == null){
////        如果查询结果为NULL 说明不存在该学生 则给该学生赋值插入
////           创建checkall对象
//                checkallStudent.setNumber("1");
//                checkallStudentMapper.insert(checkallStudent);
//            }else{
//                Integer num = Integer.parseInt(c1.getNumber());
//                num++;
//                checkallStudent.setNumber(num.toString());
//                checkallStudentMapper.updateByPrimaryKey(checkallStudent);
//            }

        if(checkTeacher.getId() != null ){
            checkTeacherMapper.updateByPrimaryKey(checkTeacher);
        }else {
            checkTeacherMapper.insert(checkTeacher);
        }

    }

    @Override
    public void deleteById(long id) {
        checkTeacherMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<CheckTeacherExtend> findWithAll(int pageNum,int limit) {
        PageHelper.startPage(pageNum,limit);
        CheckTeacherExample checkStudentExample = new CheckTeacherExample();
        List<CheckTeacherExtend> list1 = checkTeacherExtendMapper.findWithAll();
        PageInfo<CheckTeacherExtend> data = new PageInfo<CheckTeacherExtend>(list1);
        List<CheckTeacherExtend> list = data.getList();
        return list;
    }

    @Override
    public List<CheckTeacherExtend> findWithAllByTeacherId(Long teacherId,Long classesId,String type) {
        return checkTeacherExtendMapper.findWithAllByTeacherId(teacherId,classesId,type);
    }


}